import { Controller, Get, Post, Body, Param, Delete } from "@nestjs/common";
import { MenuService } from "./menu.service";
import { Permission } from "@decorators";
import { BatchDeleteDto } from "@dto/public.dto";
import { CreateMenuDTO, CreateMenuGroupDTO, VerifyNameDTO } from "@dto/menu.dto";
@Controller('menu')
export class MenuController {
    constructor(readonly menuService: MenuService) {}

    /**
     * 获取菜单列表
     */
    @Get()
    @Permission("QUERY_MENU_LIST")
    getMenuList() {
        return this.menuService.getMenuList()
    }

    /**
     * 根据父亲id获取子集
     * @param {number} pid
     */
    @Get('list/:pid')
    getMenuListByParentId(@Param('pid') pid: string) {
        return this.menuService.getMenuListByParentId(pid === 'root' ? 0 : +pid)
    }

    /**
     * 获取菜单选项列表
     */
    @Get('options/:pid')
    getMenuSelectOptions(@Param('pid') pid: string) {
    return this.menuService.getMenuSelectOptions(pid === 'root' ? 0 : +pid)
    }

    /**
     * 创建菜单
     * @param {CreateMenuDTO} data
     */
    @Post()
    @Permission("CREATE_MENU")
    createMenu(@Body() data: CreateMenuDTO) {
        return this.menuService.createMenu(data)
    }

    /**
     * 删除菜单
     * @param {number} id
     */
    @Delete('/delete/:id')
    @Permission("MENU_DELETE")
    deleteMenu(@Param('id') id: string) {
        return this.menuService.deleteMenu(+id)
    }

    @Delete('/batch')
    @Permission("MENU_BATCH_DELETE")
    batchDeleteMenu(@Body() data: BatchDeleteDto) {
        return this.menuService.batchDeleteMenu(data)
    }

    /**
     * 验证名称是否重复
     * @param {VerifyNameDTO} data
     */
    @Post('/check_name')
    checkName(@Body() data: VerifyNameDTO) {
        return this.menuService.checkName(data)
    }

    /**
     * 创建分组
     * @param {CreateMenuGroupDTO} data
     */
    @Post('/group')
    createGroup(@Body() data: CreateMenuGroupDTO) {
        return this.menuService.createGroup(data)
    }

    /**
     * 获取分组下的用户数
     */
    @Post('/group/user_count')
    getGroupUserCount() {
        return this.menuService.getGroupUserCount()
    }

    /**
     * 生成前端路由
     * @param {number} userId 用户id
     */
    @Get('/build/:userId')
    buildMenu(@Param('userId') userId: string) {
        return this.menuService.buildMenu(+userId);
    }
}
